<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\EduActiveOrder;

/**
 * EduActiveOrderSearch represents the model behind the search form of `backend\models\EduActiveOrder`.
 */
class EduActiveOrderSearch extends EduActiveOrder
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'active_id', 'count', 'status', 'create_time', 'sex', 'make_time', 'day_up', 'fill_in'], 'integer'],
            [['openid', 'out_trade_no', 'name', 'phone', 'address', 'logistics', 'order_num', 'add_phone', 'add_name', 'experience', 'follow_teach', 'puzzled', 'perfect', 'brand'], 'safe'],
            [['total_fee', 'logistic_fee'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = EduActiveOrder::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => ['create_time' => SORT_DESC]
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'user_id' => $this->user_id,
            'active_id' => $this->active_id,
            'total_fee' => $this->total_fee,
            'logistic_fee' => $this->logistic_fee,
            'count' => $this->count,
            'status' => $this->status,
            'create_time' => $this->create_time,
            'sex' => $this->sex,
            'make_time' => $this->make_time,
            'day_up' => $this->day_up,
            'fill_in' => $this->fill_in,
        ]);

        $query->andFilterWhere(['like', 'openid', $this->openid])
            ->andFilterWhere(['like', 'out_trade_no', $this->out_trade_no])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'logistics', $this->logistics])
            ->andFilterWhere(['like', 'order_num', $this->order_num])
            ->andFilterWhere(['like', 'add_phone', $this->add_phone])
            ->andFilterWhere(['like', 'add_name', $this->add_name])
            ->andFilterWhere(['like', 'experience', $this->experience])
            ->andFilterWhere(['like', 'follow_teach', $this->follow_teach])
            ->andFilterWhere(['like', 'puzzled', $this->puzzled])
            ->andFilterWhere(['like', 'perfect', $this->perfect])
            ->andFilterWhere(['like', 'brand', $this->brand]);

        return $dataProvider;
    }
}
